/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-09
 * Time: 04:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

using tools;

using Labb4.Main;

namespace Labb4.Entity
{
	/// <summary>
	/// This class wraps a macro of control pad key presses, and plays them when activated.
	/// </summary>
	public class ControlPadMacro : ControlPad
	{
		#region members
		
		/// <summary>
		/// The macros
		/// </summary>
		private List<KeyCommand>		m_Macro;
		/// <summary>
		/// A counter for knowing when to play the next key macro. Unit is (ms)
		/// </summary>
		private int						m_Counter;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, initializes members, and loads the macro from file
		/// </summary>
		/// <param name="configFile"></param>
		public ControlPadMacro(string configFile) :
			base()
		{
			m_Macro = new List<KeyCommand>();
			
			Config config = new Config();
			
			if(MainForm.ErrorCheck(config.Open(configFile)))
			{
				bool turnSpecs = false;
				bool speedSpecs = false;
				int macros = 0;
				
				if(MainForm.ErrorCheck(config.GetBool("turn_specs", ref turnSpecs)) &&
				   MainForm.ErrorCheck(config.GetBool("speed_specs", ref speedSpecs)) &&
				   MainForm.ErrorCheck(config.GetInt("key_macros", ref macros)))
				{
					if(turnSpecs)
					{
						float soft = 0.0f;
						float medium = 0.0f;
						float hard = 0.0f;
						
						bool passed = true;
						
						passed |= MainForm.ErrorCheck(config.GetFloat("turn_soft", ref soft)) |
								  MainForm.ErrorCheck(config.GetFloat("turn_medium", ref medium)) |
								  MainForm.ErrorCheck(config.GetFloat("turn_hard", ref hard));
						
						if(speedSpecs)
						{
							float standard = 0.0f;
							float increase = 0.0f;
							float acceleration = 0.0f;
							
							passed |= MainForm.ErrorCheck(config.GetFloat("speed_standard", ref standard)) |
							          MainForm.ErrorCheck(config.GetFloat("speed_increase", ref increase)) |
							          MainForm.ErrorCheck(config.GetFloat("speed_acceleration", ref acceleration));
							
							if(passed)
							{
								SetSpecifics(soft, medium, hard, standard, increase, acceleration);
							}
						}
						else
						{
							SetSpecifics(soft, medium, hard);
						}
					}
					
					string paramPart1 = KeyCommand.ParamPart1;
					string paramPart2A = KeyCommand.ParamPart2A;
					string paramPart2B = KeyCommand.ParamPart2B;
					string paramPart2C = KeyCommand.ParamPart2C;
					
					string key = "";
					bool pressed = false;
					int time = 0;
					
					for(int i = 0; i < macros; i++)
					{
						if(MainForm.ErrorCheck(config.GetString(paramPart1 + i.ToString() + paramPart2A, ref key)) &&
						   MainForm.ErrorCheck(config.GetBool(paramPart1 + i.ToString() + paramPart2B, ref pressed)) &&
						   MainForm.ErrorCheck(config.GetInt(paramPart1 + i.ToString() + paramPart2C, ref time)))
						{
							m_Macro.Add(new KeyCommand(time, key, pressed, this));
						}
					}
				}
			}
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// Plays the macro to the controler and the entity.
		/// </summary>
		/// <param name="deltaTime">The time passed since the last update. Unit is (ms)</param>
		public override void UpdateAI(ref int deltaTime)
		{
			if(m_Entity != null)
			{
				if(m_Entity.Active)
				{
					m_Counter += deltaTime;
					
					if(m_Macro.Count > 0)
					{
						if(m_Counter > m_Macro[0].Time)
						{
							m_Macro[0].Execute();
							m_Macro.RemoveAt(0);
						}
					}
				}
			}
		}
		
		#endregion
	}
}
